package com.contract.spirit.ui.login

import android.app.Activity
import android.content.Intent
import android.text.SpannableStringBuilder
import android.view.View
import cn.woochen.comlib.util.ConfigPref
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.config.Constants
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.util.ToolUtils
import com.contract.commonlib.util.ext.showToast
import com.contract.commonlib.widget.ShowAllSpan
import com.contract.spirit.R
import com.contract.spirit.databinding.ActivityLoginBinding
import com.contract.spirit.manage.CommonConfigManager
import com.contract.spirit.ui.invite.InputInviteCodeActivity
import com.contract.spirit.ui.webview.CommonWebViewActivity
import com.zrq.spanbuilder.SpanBuilder

/**
 * @author: witness
 * created: 2021/9/18
 * desc:
 */
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {


    override fun setContentView(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        val phone by ConfigPref(Constants.USER_PHONE, "")
        if (phone.isNotBlank()) {
            mViewModel.phoneNumber.set(phone)
        }
        mBinding.model = mViewModel
        initAggreement()
    }

    private fun initAggreement() {
        val spannableStringBuilder = SpannableStringBuilder()
        spannableStringBuilder.append(
            SpanBuilder(
                "注册/登录即同意协议",
                12,
                resources.getColor(R.color.colorBtnGrayEnd)
            )
        )
            .append(
                SpanBuilder("《用户协议》", 12, resources.getColor(R.color.colorBtnOrangeStart))
                    .setClick(
                        mBinding.tvAgreement,
                        ShowAllSpan(
                            mContext,
                            R.color.colorBtnOrangeStart,
                            object : ShowAllSpan.OnAllSpanClickListener {
                                override fun onClick(widget: View?) {
                                    CommonWebViewActivity.goLaunch(
                                        mContext,
                                        "用户协议",
                                        CommonConfigManager.configInfoResponse.userAgreementUrl
                                    )
                                }
                            })
                    )
            )
            .append(SpanBuilder("和", 12, resources.getColor(R.color.colorBtnGrayEnd)))
            .append(
                SpanBuilder("《隐私政策》", 12, resources.getColor(R.color.colorBtnOrangeStart))
                    .setClick(
                        mBinding.tvAgreement,
                        ShowAllSpan(
                            mContext,
                            R.color.colorBtnOrangeStart,
                            object : ShowAllSpan.OnAllSpanClickListener {
                                override fun onClick(widget: View?) {
                                    CommonWebViewActivity.goLaunch(
                                        mContext,
                                        "隐私政策",
                                        CommonConfigManager.configInfoResponse.privacyAgreementUrl
                                    );
                                }
                            })
                    )
            );
        mBinding.tvAgreement.text = spannableStringBuilder
    }

    override fun initListener() {
        mBinding.btnLogin.setOnSingleClickListener {
            checkRegister()
        }
        mBinding.tvCode.setOnSingleClickListener {
            if (mViewModel.phoneNumber.get().isNullOrEmpty()) {
                showToast("请输入手机号")
                mBinding.etPhone.setShakeAnimation()
            } else if (!ToolUtils.isMobileNum(mViewModel.phoneNumber.get().toString())) {
                showToast("请输入正确手机号")
                mBinding.etPhone.setShakeAnimation()
            } else {
                getEmsCode()
            }
        }
    }

    private fun checkRegister() {
        mViewModel.checkRegister(mViewModel.phoneNumber.get().toString())
    }

    private fun getEmsCode() {
        mViewModel.getMmsCode()
    }

    private fun login() {
        mViewModel.login("")
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.apply {
            emsCodeObservel.observe(mContext, {
                parseState(it, {
                    mBinding.tvCode.start()
                })
            })
            LoginManger.registerLoginObervable(this, mContext)
            registeCheckObservable.observe(mContext, { it ->
                parseState(it, {
                    if (it.status == 0) {
                        //未注册
                        InputInviteCodeActivity.launch(
                            phoneNumber.get().toString(),
                            smsCode.get().toString(),
                            mContext
                        )
                    } else {
                        //已注册
                        login()
                    }
                })
            })
        }
    }

    companion object{
        fun launch(activity: Activity){
            activity.startActivity(Intent(activity, LoginActivity::class.java))
        }
    }
}